//询价审核

interface IInquiryAuditManagentViewModel extends IBaseViewModel<IInquiryAuditQueryTerms> {
    /**
     * ui-grid控制
     */
    gridOptions: uiGrid.IGridOptionsOf<InquiryListItemResponse>;
    /**
     * 时间日期格式
     */
    dateFormat: "{format:'YYYY.MM.DD HH:mm'}";
    /**
     * 调度专员下拉 
     */
    dispatchOfficerDropDown: EmployeeListItemResponse[];
    /**
     * 询价状态下拉
     */
    inquiryStatusDropDown: ValueListItemResponse[];
    /**
     * 询价审核事件
     */
    inquiryAuditEdit: (id: string) => void;
    /**
     * 列表高
     */
    heightA:number;
}

interface IInquiryAuditQueryTerms {
    /**
     * 总询价单编号
     */
    inquiryId: string;
    /**
     * 询价子单编号
     */
    inquiryChildId: string;
    /**
     * 客户单位
     */
    clientName: string;
    /**
     * 调度专员
     */
    dispatchOfficer: string;
    /**
     * 开始时间
     */
    beginTime: string;
    /**
     * 结束时间
     */
    endTime: string;
    /**
     * 发货地址
     */
    startAddress: string;
    /**
     * 送货地址
     */
    endAddress: string;
    /**
     * 询价状态
     */
    states: string;
}

class InquiryAuditController extends ListController<IInquiryAuditManagentViewModel, IInquiryAuditQueryTerms>{
    gridApi: uiGrid.IGridApiOf<InquiryListItemResponse>;
    orderBy: number = 7;
    asc: boolean = false;
    /**
     * 询价状态
     */
    inquiryState:string="6";

    constructor($scope: IInquiryAuditManagentViewModel, private inquiryService: IInquiryService, private valueService: IValueService, private $ngBootbox: BootboxService,
        private employeeService: IEmployeeService, private $state: angular.ui.IStateService,private $location:ng.ILocationService) {
        super($scope);
        this.init();
    }

    init(): void {
        super.init();
       
        this.$scope.inquiryAuditEdit = this.inquiryAuditEdit;
        this.$scope.queryTerms = {
            inquiryId: "",
            inquiryChildId: "",
            clientName: "",
            dispatchOfficer: "",
            beginTime: "",
            endTime: "",
            startAddress: "",
            endAddress: "",
            states: "6,8,11"
        }

        this.$scope.gridOptions = {
            paginationPageSizes: [10, 20, 30],
            useExternalPagination: true,
            useExternalSorting: true,
            enableColumnResizing: true,
            columnDefs: [
                { displayName: "总询价编号", field: 'inquiryId', enableColumnMenu: false, width: '9%' },
                { displayName: "子询价编号", field: 'inquiryChildId', enableColumnMenu: false, width: '10%' },
                { displayName: "客户单位", field: 'clientName', enableColumnMenu: false, width: '10%' },
                { displayName: '发货地址', field: 'startAddress', enableColumnMenu: false, enableSorting: false, width: '10%' },
                { displayName: '送货地址', field: 'endAddress', enableColumnMenu: false, enableSorting: false, width: '10%' },
                { displayName: '货物名称', field: 'goodsName', enableColumnMenu: false },
                { displayName: '货物数量', field: 'goodsQuantity', enableColumnMenu: false },
                { displayName: '所需车长', field: 'carLength', enableColumnMenu: false, },
                { displayName: '调度专员', field: 'dispatchOfficer', enableColumnMenu: false },
                { displayName: '下单时间', field: 'createTime', enableColumnMenu: false, width: '9%' },
                { displayName: '紧急程度', field: 'urgency', enableColumnMenu: false },
                { displayName: '询价状态', field: 'state', enableColumnMenu: false },
                {
                    displayName: "操作",
                    field: 'state',
                    cellTemplate: '<a ng-click="grid.appScope.inquiryAuditEdit(row.entity)" class="fa fa-edit m-l-xs  primary text-info" title="询价审核"></a>',
                    enableColumnMenu: false, enableSorting: false, width: '6%'
                }
            ],
            onRegisterApi: (gridApi) => {
                this.gridApi = gridApi;
                gridApi.pagination.on.paginationChanged(this.$scope, this.paginationChanged);
                gridApi.core.on.sortChanged(this.$scope, this.sortChange);
            }
        }

        this.query();
        this.loadDispatchOfficerDropDown();
        this.loadInquiryStatusDropDown();
        this.$scope.$on("WildDog:Inquiry4", this.onWilddogOrderEvent);

        $('#opendataTime').click(function () {
            $('#dataTime').datetimepicker('show');
        });
        $('#opendataTimeEnd').click(function () {
            $('#dataTimeEnd').datetimepicker('show');
        });
    }

    localHistory():void{
        super.localHistory(this.$state);
    }
    getHistory():void{
        super.getHistory(this.$state,this.$location)
    }


    /**
     * 加载数据
     */
    loadData(queryTerms: IInquiryAuditQueryTerms, skip: number, count: number): void {
        if(queryTerms.states == " " || queryTerms.states == undefined ) { queryTerms.states = "6,8,11" };
        this.inquiryService.getList(this.orderBy.toString(), this.asc, queryTerms.inquiryId, queryTerms.inquiryChildId, queryTerms.clientName, queryTerms.dispatchOfficer, "",
            queryTerms.startAddress, queryTerms.endAddress, queryTerms.states, queryTerms.beginTime, queryTerms.endTime, skip, count).then(result => {
                this.$scope.gridOptions.totalItems = result.data.total;
                this.$scope.gridOptions.data = result.data.data;
                
                this.$scope.heightA = (this.$scope.gridOptions.data.length *39)+95;

                if (skip == 0) {
                    this.$scope.gridOptions.paginationCurrentPage = 1;
                }
                this.$scope.querying = false;
            }, error => {
                this.$scope.querying = false;
            });
    }

    /**
     * 排序事件触发
     */
    sortChange = (grid: uiGrid.IGridInstanceOf<InquiryListItemResponse>, sortColumns: Array<uiGrid.IGridColumnOf<InquiryListItemResponse>>) => {
        if (sortColumns.length == 0) {
            this.reload();
        } else {
            let sortItem = sortColumns[0];
            switch (sortItem.name) {
                case "inquiryId": {
                    this.orderBy = 1;
                } break;
                case "inquiryChildId": {
                    this.orderBy = 2;
                } break;
                case "clientName": {
                    this.orderBy = 3;
                } break;
                case "goodsName": {
                    this.orderBy = 4;
                } break;
                case "goodsQuantity": {
                    this.orderBy = 5;
                } break;
                case "carLength": {
                    this.orderBy = 6;
                } break;
                case "createTime": {
                    this.orderBy = 7;
                } break;
                case "urgency": {
                    this.orderBy = 8;
                } break;
                case "state": {
                    this.orderBy = 9;
                } break;
            }
            this.asc = sortItem.sort.direction == "asc";
            this.reload();
        }
    }

    /**
     * 监听野狗事件
     */
    onWilddogOrderEvent = (event: ng.IAngularEvent, data: any) => {
        this.reload();
    }

    /**
     * 调度专员下拉
     */
    loadDispatchOfficerDropDown(): void {
        this.employeeService.getbytype(CommissionerType.Dispatch.toString(), 0, -1).then(result => {
            this.$scope.dispatchOfficerDropDown = result.data.data;
        });
    }

    /**
     * 询价状态下拉
     */
    loadInquiryStatusDropDown(): void {
        let InquiryStatus = this.valueService.getInquiryStatusList().data;
        InquiryStatus.splice(0,4);
        InquiryStatus.splice(1,1);
        InquiryStatus.splice(2,2);
        InquiryStatus.splice(3,1);
        this.$scope.inquiryStatusDropDown = InquiryStatus;
    }

    /**
     * 询价审核事件
     */
    inquiryAuditEdit=(item) => {
        if (item.state == "已审核") {
            this.$state.go("app.inquiry.Inquiry_pay", { id: item.id, name: 'particularsTow' })
        } else if (item.state == "待审核") {
            this.$state.go("app.inquiry.Inquiry_pay", { id: item.id, name: 'particularsOne' });
        } else if (item.state == "中标待定") {
            this.$state.go("app.inquiry.Inquiry_pay", { id: item.id, name: 'undetermined' });
        }
    }
}

angular.module("tms").controller("inquiryAuditController", ["$scope", "inquiryService", "valueService", "$ngBootbox", "employeeService", "$state","$location", InquiryAuditController]);